﻿using _2._18._2015_MoviesInClass.Adapters;
using _2._18._2015_MoviesInClass.Data;
using _2._18._2015_MoviesInClass.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _2._18._2015_MoviesInClass.Controllers
{
    [Authorize(Roles="Admin")]
    public class MovieActorController : Controller
    {
        private IMovieAdapter _adapter;

        public MovieActorController(IMovieAdapter adapter)
        {
            _adapter = adapter;
        }

        // GET: MovieActor
        public ActionResult Index()
        {
            MovieListViewModel model = new MovieListViewModel();

            model.Movies = _adapter.GetMovies();

            return View(model);
        }

        public ActionResult Create()
        {
            CreateMovieActorViewModel model = new CreateMovieActorViewModel();
            HydrateLookups(model);

            return View(model);
        }

        public void HydrateLookups(CreateMovieActorViewModel model)
        {
            using (MoviesDbContext db = new MoviesDbContext())
            {
                model.Actors = db.Actors.Select(a => new SelectListItem<int>
                {
                    Name = a.FirstName + " " + a.LastName,
                    Value = a.ActorId
                }).ToList();

                model.Movies = db.Movies.Select(m => new SelectListItem<int>
                {
                    Name = m.Title,
                    Value = m.MovieId
                }).ToList();
            }
        }
    }
}